package com.sample.livewallpaperdemo;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;

import org.rajawali3d.Object3D;
import org.rajawali3d.animation.Animation3D;
import org.rajawali3d.animation.RotateAnimation3D;
import org.rajawali3d.animation.RotateOnAxisAnimation;
import org.rajawali3d.lights.PointLight;
import org.rajawali3d.loader.ALoader;
import org.rajawali3d.loader.LoaderOBJ;
import org.rajawali3d.loader.async.IAsyncLoaderCallback;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.renderer.Renderer;
import org.rajawali3d.util.RajLog;

/**
 * Created by jtxiang on 17-7-5.
 */

public class ObjRenderer extends Renderer implements IAsyncLoaderCallback {

    private PointLight mLight;

    public ObjRenderer(Context context) {
        super(context);
    }

    @Override
    protected void initScene() {
        getCurrentScene().setBackgroundColor(Color.BLUE);

        mLight = new PointLight();
        mLight.setPosition(0, 0, 4);
        mLight.setPower(3);
        getCurrentScene().addLight(mLight);

        final LoaderOBJ loaderOBJ = new LoaderOBJ(mContext.getResources(),
                mTextureManager, R.raw.multiobjects_obj);
        loadModel(loaderOBJ, this, R.raw.multiobjects_obj);

        getCurrentCamera().enableLookAt();
        getCurrentCamera().setLookAt(0, 0, 0);
        getCurrentCamera().setZ(15);
        getCurrentCamera().setOrientation(getCurrentCamera().getOrientation().inverse());
    }

    @Override
    public void onRender(final long elapsedTime, final double deltaTime) {
        super.onRender(elapsedTime, deltaTime);
        //mSphere.rotate(Vector3.Axis.Y, 1.0);
    }

    public void onTouchEvent(MotionEvent event){
    }

    public void onOffsetsChanged(float x, float y, float z, float w, int i, int j){
    }

    @Override
    public void onModelLoadComplete(ALoader loader) {
        final LoaderOBJ obj = (LoaderOBJ) loader;
        final Object3D parsedObject = obj.getParsedObject();
        parsedObject.setPosition(Vector3.ZERO);
        getCurrentScene().addChild(parsedObject);

        // 动画
        Animation3D anim = new RotateOnAxisAnimation(Vector3.Axis.Y, 360);
        anim.setDurationMilliseconds(5000);
        anim.setTransformable3D(parsedObject);
        anim.setRepeatCount(1000000);
        anim.setRepeatMode(Animation3D.RepeatMode.REVERSE);
        anim.setInterpolator(new LinearInterpolator());
        getCurrentScene().registerAnimation(anim);
        anim.play();
    }

    @Override
    public void onModelLoadFailed(ALoader loader) {
        RajLog.e("Model load failed: " + loader);
    }
}
